package site.isscloud.web.controller.project;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.isscloud.common.annotation.Log;
import site.isscloud.common.core.controller.BaseController;
import site.isscloud.common.core.domain.AjaxResult;
import site.isscloud.common.core.page.TableDataInfo;
import site.isscloud.common.enums.BusinessType;
import site.isscloud.common.utils.poi.ExcelUtil;
import site.isscloud.project.domain.PjAlarmRecord;
import site.isscloud.project.service.IPjAlarmRecordService;

import static site.isscloud.common.utils.ServletUtils.getRequest;

/**
 * 告警记录Controller
 */
@RestController
@RequestMapping("/project/record")
public class AlarmRecordController extends BaseController
{
    @Autowired
    private IPjAlarmRecordService alarmRecordService;

    /**
     * 查询告警记录列表
     */
    @PreAuthorize("@ss.hasPermi('project:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(PjAlarmRecord pjAlarmRecord, HttpServletRequest request)
    {
        // 设置时间范围参数到 params 中
        Map<String, Object> params = pjAlarmRecord.getParams();
        if (params == null) {
            params = new HashMap<>();
            pjAlarmRecord.setParams(params);
        }

        // 直接从请求参数中获取
        String beginTime = request.getParameter("params[beginTime]");
        String endTime = request.getParameter("params[endTime]");

        if (beginTime != null && !beginTime.trim().isEmpty()) {
            params.put("beginTime", beginTime);
        }
        if (endTime != null && !endTime.trim().isEmpty()) {
            params.put("endTime", endTime);
        }

        // 获取米标范围参数
        String minMeterMark = request.getParameter("minMeterMark");
        String maxMeterMark = request.getParameter("maxMeterMark");
        if (minMeterMark != null && !minMeterMark.trim().isEmpty()) {
            params.put("minMeterMark", Integer.parseInt(minMeterMark));
        }
        if (maxMeterMark != null && !maxMeterMark.trim().isEmpty()) {
            params.put("maxMeterMark", Integer.parseInt(maxMeterMark));
        }

        // 获取断纤位置范围参数
        String minFiberBreakPosition = request.getParameter("minFiberBreakPosition");
        String maxFiberBreakPosition = request.getParameter("maxFiberBreakPosition");
        if (minFiberBreakPosition != null && !minFiberBreakPosition.trim().isEmpty()) {
            params.put("minFiberBreakPosition", Integer.parseInt(minFiberBreakPosition));
        }
        if (maxFiberBreakPosition != null && !maxFiberBreakPosition.trim().isEmpty()) {
            params.put("maxFiberBreakPosition", Integer.parseInt(maxFiberBreakPosition));
        }

        startPage();
        List<PjAlarmRecord> list = alarmRecordService.selectPjAlarmRecordList(pjAlarmRecord);
        return getDataTable(list);
    }

    /**
     * 导出告警记录列表
     */
    @PreAuthorize("@ss.hasPermi('project:record:export')")
    @Log(title = "告警记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PjAlarmRecord pjAlarmRecord)
    {
        List<PjAlarmRecord> list = alarmRecordService.selectPjAlarmRecordList(pjAlarmRecord);
        ExcelUtil<PjAlarmRecord> util = new ExcelUtil<PjAlarmRecord>(PjAlarmRecord.class);
        util.exportExcel(response, list, "告警记录数据");
    }

    /**
     * 获取告警记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(Long id)
    {
        return success(alarmRecordService.selectPjAlarmRecordById(id));
    }
}